<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="活动名称" prop="activityName">
        <el-input
         style="width:240px"
          v-model="queryParams.activityName"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动类型" prop="activityType">
        <el-select
        style="width: 240px"
          v-model="queryParams.activityType"
          placeholder="请选择类型"
          class="select_box"
        >
          <el-option
            :label="item.label"
            :value="item.value"
            v-for="(item, index) in customerGroupList"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="类型"  prop="noticeType">
        <el-select v-model="queryParams.noticeType" style="width:240px" placeholder="公告类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_notice_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="活动id" prop="activityId">
        <el-input
        style="width:240px"
          v-model="queryParams.activityId"
          placeholder="请输入活动id"
          clearable
        />
      </el-form-item>
      <el-form-item label="机构" prop="strategyOrgan">
        <el-cascader
          style="width: 240px"
          clearable
          @change="strategyOrganChange"
          :options="organizationList"
          :props="organizationList_props"
          v-model="strategyOrgan"
          :show-all-levels="false"
          placeholder="请选择组织机构"
        >
        </el-cascader>
      </el-form-item>
      <el-form-item label="渠道" prop="channelCode">
        <el-select v-model="queryParams.channelCode" placeholder="请选择渠道" style="width: 240px">
          <el-option
            :label="item.label"
            :value="item.value"
            v-for="(item, index) in channelList"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上下架状态" prop="formRef1">
        <el-select
          v-model="queryParams.formRef1"
          placeholder="请选择上下架状态"
          clearable
          style="width: 240px"
          multiple
          collapse-tags
        >
          <el-option
            v-for="dict in dict.type.sys_notice_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动状态" prop="formRef1">
        <el-select
          v-model="queryParams.formRef1"
          placeholder="请选择活动状态"
          clearable
          style="width: 240px"
          multiple
          collapse-tags
        >
          <el-option
            v-for="dict in dict.type.sys_notice_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="insertUser">
        <el-input
        style="width:240px"
          v-model="queryParams.insertUser"
          placeholder="请输入姓名或P13账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd1"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd2"
          v-hasPermi="['system:notice:add']"
        >新增集卡</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd3"
          v-hasPermi="['system:notice:add']"
        >新增秒杀</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- @selection-change="handleSelectionChange" -->
    <el-table v-loading="loading" :data="noticeList" >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="活动id" align="center" prop="activityId" width="100" />
      <el-table-column label="活动名称" align="center" prop="activityName" :show-overflow-tooltip="true"/>
      <el-table-column label="活动类型" align="center" prop="activityTypeName" width="100"></el-table-column>
      <el-table-column label="活动时间" align="center" prop="activityStartDateStr" width="100"></el-table-column>
      <el-table-column label="活动状态" align="center" prop="activityStatus" width="100"></el-table-column>
      <!-- <el-table-column label="公告类型" align="center" prop="status" width="100"></el-table-column> -->
      <el-table-column label="机构" align="center" prop="organName" width="100"></el-table-column>
      <el-table-column label="渠道" align="center" prop="channelName" width="100"></el-table-column>
      <el-table-column label="创建人" align="center" prop="insertUser" width="100" />
      <el-table-column label="更新时间" align="center" prop="updateTimeStr" width="100">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageNum"
      :limit.sync="pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公告标题" prop="noticeTitle">
              <el-input v-model="form.noticeTitle" placeholder="请输入公告标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公告类型" prop="noticeType">
              <el-select v-model="form.noticeType" placeholder="请选择公告类型">
                <el-option
                  v-for="dict in dict.type.sys_notice_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_notice_status"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容">
              <editor v-model="form.noticeContent" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
// import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/system/notice";
import axios from "axios";
export default {
  name: "activitySelect",
  dicts: ['sys_notice_status', 'sys_notice_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      pageNum: 1,
      pageSize: 10,
      queryParams: {},
      customerGroupList: [],
      channelList:[],
      strategyOrgan:[],
      organizationList: [],
      organizationList_props: {
        multiple: false,
        label: "organName",
        value: "organCode",
        children: "childOrganList",
        checkStrictly: true,
      },
      // 表单参数
      queryParams: {
        organId:''
      },
      form: {},
      // 表单校验
      // rules: {
        // noticeTitle: [
        //   { required: true, message: "公告标题不能为空", trigger: "blur" }
        // ],
        // noticeType: [
        //   { required: true, message: "公告类型不能为空", trigger: "change" }
        // ]
      // }
    };
  },
  created() {
    this.getList();
    this.getEnumList()
  },
  methods: {
    async getEnumList() {
      try {
        const response = await axios.post(
          process.env.VUE_APP_BASE_API + "/strategy/queryEnumList",
          {}
        );
        if (response.data.enumList) {
          if (response.data.enumList.activityType) {
            this.customerGroupList = response.data.enumList.activityType;
          }
          if (response.data.enumList.channelType) {
            this.channelList = response.data.enumList.channelType;
          }
          if (response.data.enumList.organList) {
            this.organizationList = JSON.parse(
              response.data.enumList.organList
            )
          }
        }
      } catch (error) {
        console.error("Error fetching users:", error);
      }
    },
    strategyOrganChange (val) {
      if (val.length !== 0) {
        this.queryParams.organId = val[val.length - 1];
      } else { 
        this.queryParams.organId = ''
      }
    },
    // 去除对象中为null、''的属性
    resultQueryParams (obj) {
      return Object.entries(obj).reduce((a, [k, v]) => ((v == null || v == '') ? a : ((a[k] = v), a)), {})
    },
    /** 查询列表 */
    async getList () {
      this.loading = true;
      try {
        const resultBject = this.resultQueryParams(this.queryParams)
        const response = await axios.post(process.env.VUE_APP_BASE_API +
          '/activity/queryActivityList?pageNum=' + this.pageNum + '&pageSize=' + this.pageSize, {
          ...resultBject,
          activityGroup:'up'
          });
        this.noticeList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      } catch (error) {
        this.loading = false;
        console.error('Error fetching users:', error);
      }
    },
    // 取消按钮
    // cancel() {
    //   this.open = false;
    //   this.reset();
    // },
    // 表单重置
    reset () {
      this.strategyOrgan = []
      this.queryParams = this.$options.data().queryParams
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.noticeId)
    //   this.single = selection.length!=1
    //   this.multiple = !selection.length
    // },
    /** 新增抽奖按钮操作 */
    handleAdd1() {
      this.$router.push({ path: '/activity/activityRaffle',
        // query: {
        //   activityTabTop: 'raffle',
        // }
      })
    },
    /** 新增集卡按钮操作 */
    // handleAdd2() {
      // this.$router.push({ path: '/activity/activityCardCollect',
        // query: {
        //   activityTabTop: 'cardCollect',
        // }
      // })
    // },
    /** 新增秒杀按钮操作 */
    // handleAdd3() {
      // this.$router.push({ path: '/activity/activitySeckill',
        // query: {
        //   activityTabTop: 'seckill',
        // }
      // })
    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ path: '/activity/activityRaffle',
        query: {
          activityId: row.activityId,
        }
      })
      // const noticeId = row.noticeId || this.ids
      // getNotice(noticeId).then(response => {
      //   this.queryParams = response.data;
      //   this.open = true;
      //   this.title = "修改公告";
      // });
    },
    /** 提交按钮 */
    // submitForm: function() {
      // this.$refs["form"].validate(valid => {
        // if (valid) {
          // if (this.queryParams.noticeId != undefined) {
            // updateNotice(this.queryParams).then(response => {
            //   this.$modal.msgSuccess("修改成功");
            //   this.open = false;
            //   this.getList();
            // });
          // } else {
            // addNotice(this.queryParams).then(response => {
            //   this.$modal.msgSuccess("新增成功");
            //   this.open = false;
            //   this.getList();
            // });
          // }
        // }
      // });
    // },
    /** 查看按钮操作 */
    handleDetail (row) {
      this.$router.push({ path: '/activity/activityRaffle',
        query: {
          activityId: row.activityId,
          detail: 'Y'
        }
      })
     },
    async handleDelete(row) {
      this.$modal.confirm('是否确认删除？').then(()=> {
        axios.post(process.env.VUE_APP_BASE_API +
          '/activity/insertOrUpdateActivity', {
            activityId: row.activityId,
            isDelete: '1'
        }).then(res => {
          if (res.data.code == 200) { 
            this.handleQuery();
            this.$message.success("删除成功");
          }
        })
      }).catch(error => {
        console.error("Error fetching users:", error);
      });
    }
  }
};
</script>
